﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AppNet.Models;
using AppNet.MultiLanguage;

namespace AppNet.Areas.Admin.Controllers
{
    public class CategoryController : Controller
    {
        private AppNetDBContext db = new AppNetDBContext();
        private string lang = Translation.GetInstance.Culture; 
        //
        // GET: /Admin/Category/

        public ActionResult Index()
        {
            return View(db.Categories.Where(c => c.Lang == lang).ToList());
           
        }
        //
        // GET: /Admin/Category/Details/5

        public ActionResult Details(int id = 0)
        {
            Category category = db.Categories.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // GET: /Admin/Category/Create

        public ActionResult Create()
        {
            SelectList catList = new SelectList(db.Categories.ToList(), "ID","Title");
            ViewBag.root = catList;
            return View();

        }

        //
        // POST: /Admin/Category/Create
        public ActionResult CreateSubCategories(int id)
        {
            
            Category category = db.Categories.Single(x => x.ID == id);
            return View(category);
        }

        [HttpPost]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                category.Lang = lang;
                db.Categories.Add(category);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(category);
        }

        //
        // GET: /Admin/Category/Edit/5

        public ActionResult Edit(int id = 0)
        {
            SelectList catList = new SelectList(db.Categories.ToList(), "ID", "Title");
            ViewBag.root = catList;
            Category category = db.Categories.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
         
            return View(category);
        }

        //
        // POST: /Admin/Category/Edit/5

        [HttpPost]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                category.Lang = lang;
                    if(category.IsRoot == true){
                        category.RootID = null;
                    }
                db.Entry(category).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(category);
        }

        //
        // GET: /Admin/Category/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Category category = db.Categories.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // POST: /Admin/Category/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Category category = db.Categories.Find(id);
            db.Categories.Remove(category);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}